package com.oracle.application.filter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.oracle.application.config.JwtConfig;
import com.oracle.application.vo.R;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureException;
import io.jsonwebtoken.UnsupportedJwtException;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@ConfigurationProperties(prefix = "auth")
@Data
public class AuthFilter extends ZuulFilter {

    private String[] filterUri;

    @Autowired
    private JwtConfig jwtConfig;

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext context=RequestContext.getCurrentContext();
        HttpServletRequest request=context.getRequest();
        String requestUri=request.getRequestURI();
        System.out.println(requestUri);

        System.out.println(filterUri.length);
        for (int i=0;i<filterUri.length;i++) {
            if(requestUri.startsWith(filterUri[i])){
                return true;
            }
        }
        return false;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("需要进行认证token方法");
        RequestContext context=RequestContext.getCurrentContext();
        HttpServletRequest request=context.getRequest();
        HttpServletResponse response=context.getResponse();
        String token=request.getHeader("token");
        //设置zuul网关不进行路由 并且通过zuul resposn 将内容写会json

        if (token==null||"".equals(token)){
            response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
            R r=new R(401,null,"login");
            context.setResponseBody(JSONObject.toJSONString(r));
            context.setSendZuulResponse(false);
            return null;
        }
        try {
            jwtConfig.verifyToken(token);
        } catch (ExpiredJwtException e) {
            R r=new R(402,null,"签名异常");
            context.setResponseBody(JSONObject.toJSONString(r));
            context.setSendZuulResponse(false);
        } catch (UnsupportedJwtException e) {
            R r=new R(402,null,"签名异常");
            context.setResponseBody(JSONObject.toJSONString(r));
            context.setSendZuulResponse(false);
        } catch (MalformedJwtException e) {
            R r=new R(402,null,"签名异常");
            context.setResponseBody(JSONObject.toJSONString(r));
            context.setSendZuulResponse(false);
        } catch (SignatureException e) {
            R r=new R(402,null,"签名异常");
            context.setResponseBody(JSONObject.toJSONString(r));
            context.setSendZuulResponse(false);
        } catch (IllegalArgumentException e) {
            R r=new R(402,null,"签名异常");
            context.setResponseBody(JSONObject.toJSONString(r));
            context.setSendZuulResponse(false);
        }

        return null;
    }
}
